package com.ltkj.file.controller;

import cn.hutool.core.util.StrUtil;
import com.ltkj.common.core.utils.StringUtils;
import com.ltkj.common.core.web.controller.BaseController;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.file.service.ICommAttachmentService;
import com.ltkj.system.api.domain.CommAttachment;
import com.ltkj.system.api.domain.CommAttachmentQueryVo;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ltkj.common.core.domain.R;
import com.ltkj.common.core.utils.file.FileUtils;
import com.ltkj.file.service.ISysFileService;
import com.ltkj.system.api.domain.SysFile;

import java.util.List;

/**
 * 文件请求处理
 *
 * @author ruoyi
 */
@RestController
@Api(tags = "文件相关操作")
public class SysFileController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);

    @Autowired
    private ISysFileService sysFileService;

    @Autowired
    private ICommAttachmentService commAttachmentService;

    /**
     * 文件上传请求
     */
    @PostMapping("upload")
    public R<SysFile> upload(MultipartFile file) {
        try {
            // 上传并返回访问地址
            String url = sysFileService.uploadFile(file);
            SysFile sysFile = new SysFile();
            sysFile.setName(FileUtils.getName(url));
            sysFile.setUrl(url);
            return R.ok(sysFile);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 文件上传请求
     * 存入数据库,可压缩图片
     */
    @PostMapping("uploadAtta")
    @ApiOperation("文件上传请求,需要业务主键")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "zoom", value = "是否压缩,1标识压缩", required = true),
        @ApiImplicitParam(name = "file", value = "文件", required = true)
    })
    public R<CommAttachment> uploadPlus(MultipartFile file, String zoom, CommAttachment attachment) {
        try {
            if(StrUtil.isBlank(String.valueOf(attachment.getInfoId()))){
                return R.fail("业务ID不能为空");
            }
            if(StrUtil.isBlank(attachment.getInfoCode())){
                return R.fail("业务编码不能为空");
            }
            attachment = commAttachmentService.uploadAttaData(file, zoom, attachment);
            return R.ok(attachment);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 文件上传请求
     * 存入数据库,可压缩图片
     * 返回业务数据
     */
    @PostMapping("uploadAttaData")
    @ApiOperation("文件上传请求,不需要业务主键")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "zoom", value = "是否压缩,1标识压缩", required = true),
        @ApiImplicitParam(name = "file", value = "文件", required = true)
    })
    public R<CommAttachment> uploadAttaData(MultipartFile file, String zoom, CommAttachment attachment) {
        try {
            attachment = commAttachmentService.uploadAttaData(file, zoom, attachment);
            return R.ok(attachment);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 业务id与附件id关联
     * @param attachment
     * @param attaIds
     * @return
     */
    @PostMapping("updateAttaData")
    @ApiOperation("业务id与附件id关联")
    @ApiImplicitParams({
            @ApiImplicitParam(name="attaIds", value = "附件id,多个用,分割",required = true)
    })
    public AjaxResult updateAttaData(CommAttachment attachment, String attaIds) {
        if(StringUtils.isEmpty(attaIds)) {
            return AjaxResult.error("attaIds不能为空");
        }
        if(attachment.getInfoId() == null) {
            return AjaxResult.error("InfoId不能为空");
        }
        if(StringUtils.isEmpty(attachment.getInfoCode())) {
            return AjaxResult.error("infoCode不能为空");
        }
        return commAttachmentService.updateAttaData(attachment, attaIds);
    }

    /**
     * 删除附件
     * @param attaId
     * @return
     */
    @ApiOperation("删除附件,只删表数据")
    @ApiImplicitParam(name="attaId", value = "附件id",required = true)
    @DeleteMapping("deletaAtta")
    public AjaxResult deletaAtta(Long attaId) {
        return toAjax(commAttachmentService.deleteCommAttachmentByAttaId(attaId));
    }

    /**
     * 删除附件,只删表数据,通过业务id和业务编码
     * @param attachment
     * @return
     */
    @ApiOperation("删除附件,只删表数据,通过业务id和业务编码")
    @DeleteMapping("deletaAttaByInfo")
    public AjaxResult deletaAttaByInfo(CommAttachment attachment) {
        if (StringUtils.isEmpty(attachment.getInfoCode())) {
            return AjaxResult.error("infoCode 不能为空");
        }
        if(attachment.getInfoId() == null) {
            return AjaxResult.error("infoId 不能为空");
        }
        return toAjax(commAttachmentService.deletaAttaByInfo(attachment));
    }

    /**
     * 多文件上传请求
     * 存入数据库,可压缩图片
     * 返回业务数据
     */
    @PostMapping("uploadAttaDataMore")
    @ApiOperation("多文件上传请求,不需要业务主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "zoom", value = "是否压缩,1标识压缩", required = true),
            @ApiImplicitParam(name = "files", value = "文件", required = true)
    })
    public R<List<CommAttachment>> uploadAttaDataMore(List<MultipartFile> files, String zoom, CommAttachment attachment) {
        try {
            List<CommAttachment> list = commAttachmentService.uploadAttaDataMore(files, zoom, attachment);
            return R.ok(list);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }


    @GetMapping("selectAtta")
    @ApiOperation("根据业务id和code查询附件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "infoId", value = "业务id", required = true),
            @ApiImplicitParam(name = "infoCode", value = "业务编码", required = true)
    })
    public List<CommAttachment> selectAtta(Long infoId, String infoCode) {
        CommAttachment ca = new CommAttachment();
        ca.setInfoId(infoId);
        ca.setInfoCode(infoCode);
        List<CommAttachment> list = commAttachmentService.selectCommAttachmentList(ca);
        return list;
    }

    @GetMapping("selectAttaById")
    @ApiOperation("根据附件id查询附件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "attaId", value = "业务id", required = true),
    })
    public CommAttachment selectAttaById(Long attaId) {
        CommAttachment commAttachment = commAttachmentService.selectCommAttachmentByAttaId(attaId);
        return commAttachment;
    }

    @PostMapping("selectAttaByInfoIds")
    @ApiOperation("根据多条业务id和一个code查询附件")
    public R<List<CommAttachment>> selectAttaByInfoIds(@RequestBody CommAttachmentQueryVo commAttachmentQueryVo) {
        if(commAttachmentQueryVo == null || CollectionUtils.isEmpty(commAttachmentQueryVo.getInfoIds()) || !org.springframework.util.StringUtils.hasText(commAttachmentQueryVo.getInfoCode())){
            return R.fail("请输入业务id或者code！");
        }
        List<CommAttachment> list = commAttachmentService.selectAttaByInfoIds(commAttachmentQueryVo);
        return R.ok(list);
    }
}